import 'package:flutter/material.dart';
import '../../../ui.dart';
import '../model/app_file_desc.dart';

class AppFileListItem extends StatefulWidget {
  final AppFileDesc fileDesc;
  final bool selectedMode;
  final bool isSelected;
  final VoidCallback? onTap;
  final VoidCallback? onLongPress;

  const AppFileListItem(
      {super.key,
      required this.fileDesc,
      this.selectedMode = false,
      this.isSelected = false,
      this.onTap,
      this.onLongPress});

  @override
  State<AppFileListItem> createState() => _AppFileListItemState();
}

class _AppFileListItemState extends State<AppFileListItem> {
  AppFileDesc get file => widget.fileDesc;

  Widget thumbnail() {
    if (file.isDir) {
      return const Icon(CupIcons.folder_fill, size: 42, color: defaultColor);
    } else {
      return const Icon(CupIcons.doc_fill, size: 42, color: Colors.cyan);
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: thumbnail(),
      title: MyText(
        file.name,
        textAlign: TextAlign.start,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        i18n: false,
      ),
      subtitle: Padding(
        padding: const EdgeInsets.only(top: 2),
        child: Row(
          children: [
            Expanded(
              flex: 3,
              child: MyText(
                file.isDir ? file.createTimeStr : file.createTimeStr,
                size: 12,
                weight: FontWeight.normal,
                color: JmColors.textSubColor,
                i18n: false,
              ),
            ),
            Expanded(
              flex: 2,
              child: MyText(
                file.isDir ? "" : file.sizeStr,
                size: 12,
                weight: FontWeight.normal,
                color: JmColors.textSubColor,
                i18n: false,
              ),
            ),
            Expanded(
              child: MyText(
                file.isDir ? "" : file.extension,
                size: 12,
                weight: FontWeight.normal,
                color: JmColors.textSubColor,
                overflow: TextOverflow.ellipsis,
                i18n: false,
              ),
            ),
          ],
        ),
      ),
      trailing: widget.selectedMode
          ? (widget.isSelected
              ? const Icon(
                  CupIcons.checkmark_circle_fill,
                  size: 22,
                  color: defaultColor,
                )
              : const Icon(
                  CupIcons.circle,
                  size: 22,
                  color: defaultTextSubColor,
                ))
          : (file.isDir
              ? Icon(
                  CupIcons.chevron_right,
                  size: 22,
                  color: JmColors.textSubColor,
                )
              : const SizedBox(
                  width: 22,
                )),
      onTap: widget.onTap,
      onLongPress: widget.onLongPress,
    );
  }
}
